<script>
function goBack(){
    window.location.href = "?modul=products&action=products_type";
}
</script>
<?php

function V_Add_Products_Type($arrayData, $mode) {
    include("../fckeditor/fckeditor.php");
    $ulr = "?" . MODUL . "=products&" . ACTION . "=" . $mode;
    if ($mode == 'Edit_type') {
	$strLable = "Cập nhật loại sản phẩm";
	$strButton = "Cập nhật";
    } else if ($mode == 'add_products_type') {
	$strLable = "Thêm mới loại sản phẩm";
	$strButton = "Thêm mới";
    }
    ?>
    <h3><?= $strLable ?></h3>
    <form enctype="multipart/form-data" action="<?= $ulr ?>" method="post" name="frm" onSubmit="return validateForm();">
        <input type="hidden" name="type_id" id="type_id" value="<?= $arrayData['id']; ?>"/>
        <table width="950px" cellpadding="5" border="1">
    	<tr style="background-color: #99CCFF">
    	    <th colspan="2">Thông tin loại sản phẩm</th>
    	</tr>
    	<tr>
    	    <td width="200px"><strong>Chọn nhóm</strong></td>
    	    <td>
		<select name="type_parent">
    		    <option value="0">---ROOT---</option>
			<? products_type(0, 0, $arrayData['type_parent']); ?>
    		</select>
    	    </td>
    	</tr>
    	<tr>
    	    <td><strong>Tên loại</strong></td>
    	    <td>
    		<input type="text" name="type_name" id="type_name" value="<?= $arrayData['type_name']; ?>"/>
    	    </td>
    	</tr>
    	<tr>
    	    <td><strong>Ghi chú</strong></td>
    	    <td>
    		<textarea style="width: 500px; height: 250px" name="type_note" id="type_note"><?= $arrayData['type_note']; ?></textarea>
    	    </td>
    	</tr>
		<tr>
    	    <td><strong>Chi tiết sản phẩm</strong></td>
    	    <td>
			<?php
		    // Lay ve duong dan chinh cua server.
		    $sBasePath = $_SERVER['PHP_SELF'];

		    // Lay ve thu muc goc cho den _sample.
		    $sBasePath = substr($sBasePath, 0, strpos($sBasePath, "run"));
		    //echo "sBasePath = $sBasePath";
		    $sBasePath = $sBasePath . "fckeditor/";
		    $oFCKeditor = new FCKeditor('type_detail');
		    $oFCKeditor->BasePath = $sBasePath;
		    $oFCKeditor->Width = '100%';
		    $oFCKeditor->Height = '400';
		    $oFCKeditor->Value = $arrayData["type_detail"];
		    $oFCKeditor->Create();
		    ?>
    	    </td>
    	</tr>
    	<tr>
    	    <td colspan="2">
    		<div style="float: right">
    		    <input type="reset" name="reset"  value="Nhập lại"/>
    		    <input type="submit" name="Submit" value="<?= $strButton ?>"/>
		    <input type="button" name="Back" value="Quay lại" onclick="goBack();"/>
    		</div>
    	    </td>
    	</tr>
        </table>
    </form>
    <?
}

function products_type($parent_id, $level, $id_nhom) {
    global $db;
    $objProducts = new MD_Products($db);
    $where = " type_parent = '$parent_id'";
    $order = "";
    $limit = "";
    $array = $objProducts->get_product_type_by_Where($where, $order, $limit);
    foreach ($array as $value) {
	$id = $value['id'];
	$selected = "";
	if ($id_nhom == $id) {
	    $selected = "Selected";
	}
	echo '<option ' . $selected . ' value="' . $id . '">' . str_repeat('- - ', $level) . $value['type_name'] . '</option>';
	products_type($id, $level + 1, $id_nhom);
    }
}
?>
